package com.wentao.thisService.controller;

import com.wentao.thisCommon.annotation.MethodAuthority;
import com.wentao.thisCommon.model.RequestBase;
import com.wentao.thisCommon.model.ResultBase;
import com.wentao.thisFacade.business.FileUploadCtrInterface;
import com.wentao.thisFacade.common.enums.ExceptionCode;
import com.wentao.thisFacade.common.enums.SuccessCode;
import com.wentao.thisFacade.domain.reqeust.HotelDetailsRequest;
import com.wentao.thisFacade.domain.result.FileResult;
import com.wentao.thisFacade.domain.result.UploadResult;
import com.wentao.thisFacade.domain.result.VersionResult;
import com.wentao.thisService.common.TokenVerify;
import com.wentao.thisService.common.upload.UploadFileInfo;
import com.wentao.thisService.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
public class FileUploadController implements FileUploadCtrInterface {

    @Autowired
    private FileService fileService;

    @SuppressWarnings("unchecked")
//    @MethodAuthority(permissionCode = {"ADMIN_ALLOW_FUNC"})
    public ResultBase<UploadFileInfo> uploadHDFS(@RequestParam("file") MultipartFile file_update) throws Exception {
//        UploadFileInfo upload_path = fileExecutor.uploadHdfs(file_update, ConstString.UPLOAD_File);
//        if (null != upload_path) {
//            return ResultBase.ok(upload_path, SuccessCode.SYSTEM_SUCCESS);
//        }
        return ResultBase.ok(ExceptionCode.UPLOAD_FILE_TYPE_ERROR);
    }

    /**
     * @see FileUploadCtrInterface#upload(MultipartFile, String, String)
     */
    @SuppressWarnings("unchecked")
    @Transactional
    @Override
    public ResultBase<UploadResult> upload(@RequestParam("file") MultipartFile file, @RequestParam("uploadType") String uploadType, @RequestParam("uploadCode") String uploadCode) throws Exception {
        UploadResult uploadResult = fileService.upload(file, uploadType, uploadCode);
        return ResultBase.ok(uploadResult, SuccessCode.SYSTEM_SUCCESS);
    }

    /**
     * @see FileUploadCtrInterface#upload(MultipartFile, String, String)
     */
    @SuppressWarnings("unchecked")
    @Transactional
    @Override
    public ResultBase<UploadResult> uploadOSS(@RequestParam("file") MultipartFile file, @RequestParam("uploadType") String uploadType, @RequestParam("uploadCode") String uploadCode) throws Exception {
        UploadResult uploadResult = fileService.uploadOSS(file, uploadType, uploadCode);
        return ResultBase.ok(uploadResult, SuccessCode.SYSTEM_SUCCESS);
    }

    /**
     * @see FileUploadCtrInterface#hotelFiles(RequestBase)
     */
    @SuppressWarnings("unchecked")
    @Override
    @TokenVerify
    public ResultBase<List<FileResult>> hotelFiles(RequestBase<HotelDetailsRequest> entity) throws Exception {
        // TODO: 2020/3/27 权限验证
        List<FileResult> fileResultList = fileService.hotelFiles(entity.getBody());
        return ResultBase.ok(fileResultList, SuccessCode.SYSTEM_SUCCESS);
    }

    /**
     * @see FileUploadCtrInterface#saveAPK(MultipartFile, Integer, String, String, Integer, Integer, String)
     */
    @SuppressWarnings("unchecked")
    @Override
    public ResultBase<VersionResult> saveAPK(MultipartFile file, Integer versionCode, String versionName, String updateContent, Integer compatibleMinCode, Integer compatibleMaxCode, String createBy) throws Exception {

        VersionResult versionResult = fileService.saveAPK(file, versionCode, versionName, updateContent, compatibleMinCode, compatibleMaxCode, createBy);

        return ResultBase.ok(versionResult, SuccessCode.SYSTEM_SUCCESS);
    }
}
